//
//  FindDetailViewController.m
//  Zaker
//
//  Created by dllo on 16/3/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FindDetailViewController.h"

@interface FindDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;

@end

@implementation FindDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    [self createTableView];
}

- (void)handleData
{
    NSLog(@"%@", _path);
    [LHAFNetworkTool getUrl:_path body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *arr = resposeObject[@"data"][@"posts"];
        self.dataArr = [FindDetailModel modelHandlerWithArray:arr];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    [_tableView.mj_header endRefreshing];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    self.navigationItem.title = _titleName;
    [_tableView registerClass:[FindDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    //数据处理
    [self handleData];
    //下拉刷新
    //话题页下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleData)];
    [_tableView.mj_header beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailModel *m = _dataArr[indexPath.row];
    CGFloat h = [FindDetailTableViewCell heightWithText:m.content];
    CGFloat pictureH = [FindDetailTableViewCell heightWithImage:m.width WithHeight:m.height];
    if (pictureH < 400) {
        pictureH = pictureH;
    } else {
        pictureH = 350;
    }
    return h + pictureH  + 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    FindDetailModel *m = _dataArr[indexPath.row];
    cell.pictureStr = m.autherImg;
    [cell.userImg sd_setImageWithURL:[NSURL URLWithString:m.autherImg] placeholderImage:[UIImage imageNamed:@"User_Profiles_bg"]];
    cell.userLable.text = m.autherName;
    cell.timeLable.text = m.date;
    cell.contentLable.text = m.content;
    //传值计算自定义高度
    cell.content = m.content;
    cell.width = m.width;
    cell.height = m.height;
    
    if (m.imgPath) {
        cell.isHave = YES;
    }else{
        cell.isHave = NO;
    }
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.imgPath] placeholderImage:[UIImage imageNamed:@"User_Profiles_bg"]];
    if (m.hot_num.intValue > 1000) {
        cell.hotLabel.text = [NSString stringWithFormat:@"%.1fK", m.hot_num.floatValue / 1000];
    } else {
        cell.hotLabel.text = m.hot_num;
    }
    cell.likeLable.text = m.like_num;
    cell.messageLable.text = m.comment_count;
    cell.selectionStyle = 0;
    return cell;
}

//点击进入详情webView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //push过去时隐藏tabbar
    self.hidesBottomBarWhenPushed = YES;
    FindWebViewController *detailVC = [[FindWebViewController alloc]init];
    FindDetailModel *m = _dataArr[indexPath.row];
    detailVC.pathstr = m.weburl;
    [self.navigationController pushViewController:detailVC animated:YES];
    //pop回来时显示tabbar
    self.hidesBottomBarWhenPushed = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
